from syslogd.enums.RegexMatcher import RegexMatcher


class ConfReader(object):

    def __init__(self, fl):
        self.matcher = RegexMatcher()
        self.sFile = fl
        self.list = []

    def read(self):
        fl = open(self.sFile, 'r')

        tmp = ''
        lst = []
        getRidOfComment = False
        while 1:
            line = fl.readline()
            if not line:
                break
            line = line.strip()
            if line.endswith('\\'):
                if line[0] == '#' and getRidOfComment:
                    line = line[1:].strip()
                tmp = tmp + line [:-1]
                getRidOfComment = True
            else:
                if line != None and line != '' and line[0] == '#' and getRidOfComment:
                    line = line[1:].strip() 
                if tmp == '':
                    lst.append(line)
                else:
                    lst.append(tmp + line)
                tmp = ''
                getRidOfComment = False

        for el in lst:
            self.proceedRow(el)

    def proceedRow(self, string):
        if self.matcher.match(string):
            self.list.append(string)
        
    def getList(self):    
        return self.list
        